/*
 * QtImage.cpp
 *
 *  Created on: Mar 11, 2011
 *      Author: kmueller
 */

#include "QtImage.h"

#include <QtCore>
#include <QPainter>
#include <QPen>

QtImage::QtImage(QPainter* painter, int width, int height) {
	pWidth = width;
	pHeight = height;
	pCanvas = painter;
}

QtImage::~QtImage() {
	// TODO: destruct
}

void QtImage::set_foreground_color(opennav::Color& color) {
	pForeground.setRgb(color.r, color.g, color.b);
	pCanvas->setBrush(QBrush(pForeground, Qt::SolidPattern));
}

void QtImage::set_background_color(opennav::Color& color) {
	pBackground.setRgb(color.r, color.g, color.b);
	pCanvas->setBrush(QBrush(pBackground, Qt::SolidPattern));
}

void QtImage::set_line_width(int32_t width) {
	pCanvas->setPen(QPen(pCanvas->brush(), width, Qt::SolidLine, Qt::RoundCap));
}

void QtImage::set_font(const opennav::Font& font) {
	pFont.setPixelSize(font.size);
	switch (font.style) {
	case opennav::FONT_PLAIN: {
		pFont.setBold(false);
		pFont.setStyle(QFont::StyleNormal);
		break;
	}
	case opennav::FONT_BOLD: {
		pFont.setBold(true);
		pFont.setStyle(QFont::StyleNormal);
		break;
	}
	case opennav::FONT_ITALIC: {
		pFont.setBold(true);
		pFont.setStyle(QFont::StyleItalic);
		break;
	}
	}
}

void QtImage::draw_lines(const std::vector<opennav::Point>& polyline) {
	QVector<QPoint> points;

	for (std::vector<opennav::Point>::const_iterator it = polyline.begin(); it
			!= polyline.end(); it++) {
		points.push_back(QPoint(it->x, it->y));
	}
	pCanvas->drawPolyline(QPolygon(points));
}

void QtImage::draw_line(const opennav::Point& a, const opennav::Point& b) {
	pCanvas->drawLine(a.x, a.y, b.x, b.y);
}

void QtImage::draw_polygon(const std::vector<opennav::Point>& polyline) {
	QVector<QPoint> points;

	for (std::vector<opennav::Point>::const_iterator it = polyline.begin(); it
			!= polyline.end(); it++) {
		points.push_back(QPoint(it->x, it->y));
	}

	pCanvas->drawPolygon(QPolygon(points));
}

void QtImage::draw_rectangle(const opennav::Point& point, int32_t width,
		int32_t height) {
	pCanvas->drawRect(point.x, point.y, width, height);
}

void QtImage::draw_circle(const opennav::Point& center, int32_t radius) {
	QPoint c(center.x, center.y);
	pCanvas->drawEllipse(c, radius, radius);
}

void QtImage::draw_text(const opennav::Point& point, const std::string& text) {
	pCanvas->drawText(point.x, point.y, QString(text.c_str()));
}

void QtImage::draw_text_tilted(const opennav::Point& center,
		const std::string& text, int dx, int dy) {
	int degree;
	if (dx == 0) {
		degree = (dy == 1 ? 90 : -90);
	} else {
		degree = atanf((float) dy / (float) dx) * 180.0f / M_PI;
	}

	QString string(text.c_str());
	QFontMetrics metrics(pFont);
	QSize dim(metrics.width(string), metrics.height());

	pCanvas->save();
	pCanvas->translate(center.x-dim.height()/4, center.y+dim.height()/4);
	pCanvas->rotate(degree);
	pCanvas->drawText(0, 0, string);
	pCanvas->restore();
}

void QtImage::get_text_dimension(const std::string& text,
		opennav::Dimension& dimension) {
	QString string(text.c_str());

	QFontMetrics metric(pFont);
	dimension.height = metric.height();
	dimension.width = metric.width(string);
}

int32_t QtImage::width() {
	return pWidth;
}

int32_t QtImage::height() {
	return pHeight;
}
